//!
/************************************************************************************************************
 * @file RBRenderPassGlow.cpp
 ************************************************************************************************************/

/************************************************************************************************************
 * Precompiled Header
 ************************************************************************************************************/
#include "RBMainPCH.h"

/************************************************************************************************************
 * Includes
 ************************************************************************************************************/

#include "RBRenderPassGlow.h"

#include "RBHardwareBufferManager.h"
#include "RBRenderer.h"
#include "RBRenderProcess/RBEffectFactory.h"

/************************************************************************************************************
 * Defines
 ************************************************************************************************************/

/************************************************************************************************************
 * Implementation
 ************************************************************************************************************/
#if 0

/************************************************************************************************************
 * Constructor
 ************************************************************************************************************/
RBRenderPassGlow::RBRenderPassGlow(const RBString& strTechniqueName, RBRenderTargetGroup* pRTT)
: RBRenderPassFullScreen(strTechniqueName, pRTT)
{
    // We need to create some additional buffers so that we will reduce them
    m_pRemoveLowLuminance = RBEffectFactory::GetSingleton()->RetrieveEffect("RemoveLowLuminance");
}

/************************************************************************************************************
 * Destructor
 ************************************************************************************************************/
RBRenderPassGlow::~RBRenderPassGlow()
{
}

/***********************************************************************************************************
 * 
 ***********************************************************************************************************/
void RBRenderPassGlow::Render(uint32 /*uiFrameID*/)
{
    CheckResources();

#pragma TODO("Fix GetRenderTargetResource")
    //RBRenderer::GetSingleton()->CopyResource(RenderTargetGroup->GetRenderTargetResource()->GetBuffer(), m_spTexture1);

//     m_spMaterial->GetSampler(RBMaterial::ST_DIFFUSE).Texture = m_spTexture1;
    RenderQuad(m_pRemoveLowLuminance, m_spRTTSize4);

//     m_spMaterial->GetSampler(RBMaterial::ST_DIFFUSE).Texture = m_spTexture1;
//     m_spMaterial->GetSampler(RBMaterial::ST_SHADER0).Texture = m_spTexture4;
    RenderQuad(m_pEffect, RenderTargetGroup);

}

#if 0
static inline void CheckTexture(RBTexturePtr& spTexture, RBRenderTargetGroupPtr& spRTT, RBPixelFormat eFormat, uint32 uiWidth, uint32 uiHeight, uint32 uiFactor)
{
    if (!spTexture || (uiHeight-spTexture->GetHeight()*uiFactor) > uiFactor || (uiWidth-spTexture->GetWidth()*uiFactor) >  uiFactor /*|| spTexture->GetBuffer()->GetFormat() != eFormat*/)
    {
        spTexture = RBRenderer::GetSingleton()->GetHBMgr()->CreateTexture(RBHardwareBuffer::HBU_GPUWRITE, uiWidth/uiFactor, uiHeight/uiFactor, 
                                                                          eFormat , RBTextureType::SURFACE);

        spRTT = RBRenderTargetGroup::Create(spTexture->GetBuffer());
    }
}

/***********************************************************************************************************
 * 
 ***********************************************************************************************************/
void RBRenderPassGlow::CheckResources()
{
    uint32 uiWidth = RenderTargetGroup->GetRenderTargetResource()->->GetWidth();
    uint32 uiHeight = RenderTargetGroup->GetBuffer()->GetHeight();
    RBPixelFormat eFormat = RenderTargetGroup->GetBuffer()->GetFormat();
    CheckTexture(m_spTexture1, m_spRTTSize1, eFormat, uiWidth, uiHeight, 1);
    CheckTexture(m_spTexture4, m_spRTTSize4, eFormat, uiWidth, uiHeight, 4);
    CheckTexture(m_spTexture16, m_spRTTSize16, eFormat, uiWidth, uiHeight, 16);

}
#endif
#endif
/************************************************************************************************************/
